package com.xunua.NaoZiKongBai.Base;

import android.Manifest;
import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;

import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;
import androidx.core.app.ActivityCompat;

import com.gyf.immersionbar.ImmersionBar;
import com.xunua.NaoZiKongBai.Bean.EventBus.RealtimeloginstatusEvent;
import com.xunua.NaoZiKongBai.LoginActivity.LoginActivity;
import com.xunua.NaoZiKongBai.MyApplication;
import com.xunua.NaoZiKongBai.R;
import com.xunua.NaoZiKongBai.View.LoadProgressDialog;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;


/**
 * @user XUNUA
 * @email  xunua@outlook.com
 * @creat time 2020/5/13 12:35
 */
public abstract class BaseActivity<P extends BasePresenter,CONTRACT> extends AppCompatActivity implements BaseView {
    private final static int PERMISSION_REQUESTCODE=10086;
    public abstract CONTRACT getContract();

    public P mPresenter;

    public MyApplication myApplication;

    private LoadProgressDialog loadProgressDialog=null;

    private void permission() {//获取权限
        ActivityCompat.requestPermissions(this, new String[]{
                Manifest.permission.INTERNET,
                Manifest.permission.ACCESS_NETWORK_STATE,
                Manifest.permission.ACCESS_WIFI_STATE,
                Manifest.permission.WAKE_LOCK,
                Manifest.permission.WRITE_EXTERNAL_STORAGE,
                Manifest.permission.READ_PHONE_STATE,
                Manifest.permission.READ_EXTERNAL_STORAGE,
                Manifest.permission.WRITE_EXTERNAL_STORAGE
        }, PERMISSION_REQUESTCODE);
    }

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentViewId();
        zhuangtailang();
        mPresenter=getPresenterInstance();
        mPresenter.bindView(this);
        myApplication= (MyApplication) getApplication();
        init();
        EventBus.getDefault().register(this);
        permission();
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        EventBus.getDefault().unregister(this);
    }


    /**
     *
     * @param context
     * @param message
     * @param cancelable    true则允许点击屏幕关闭  false则只能按返回键关闭
     */
    public void showLoadingDialogBase(Context context,String message,Boolean cancelable){
        loadProgressDialog=new LoadProgressDialog(context,message,cancelable);
        loadProgressDialog.show();
    }

    public void hideLoadingDialogBase(){
        if (loadProgressDialog != null) {
            loadProgressDialog.dismiss();
        }
    }

    public void setLoadingDialogMessageBase(String message){
        if (loadProgressDialog != null) {
            loadProgressDialog.setMessage(message);
        }
    }

    public abstract void setContentViewId();//setContentView(R.layout.activity_main);

    public abstract void init();//初始化

    public abstract P getPresenterInstance();

    public abstract boolean IskeyboardEnable();//是否要解决键盘与底部输入框冲突

    public abstract View getStatusBar();//沉浸式状态栏

    private void zhuangtailang(){
        //标准模式
        ImmersionBar.with(this)
                .statusBarView(getStatusBar())
                .keyboardEnable(IskeyboardEnable())//解决软键盘与底部输入框冲突问题
                .statusBarDarkFont(true)//状态栏字体颜色
                .navigationBarColor(R.color.navigationBarcolor)//导航栏颜色，不写默认黑色
                .navigationBarDarkIcon(true)//导航栏图标是深色，不写默认为亮色
                .init();//沉浸状态栏
    }

    @Subscribe(threadMode = ThreadMode.MAIN,sticky = true)
    public void onRealtimeloginstatusEvent(RealtimeloginstatusEvent event){
        if (event.isPwdFaild()) {
            //密码错误
            //需要进行提示框  让用户退回到登录页面
            AlertDialog.Builder builder = new AlertDialog.Builder(this);
            builder.setCancelable(false);
            builder.setTitle("提示").setMessage("账号状态异常，即将下线，请重新登录").setPositiveButton("ok", new DialogInterface.OnClickListener() {
                @Override
                public void onClick(DialogInterface dialog, int which) {
                    if (myApplication.activityList.size() != 1) {
                        myApplication.finishAllActivity();//销毁所有activity
                    }else {
                        String simpleName = myApplication.activityList.get(0).getClass().getSimpleName();
                        if (simpleName.equals("LoginActivity")) {
                            EventBus.getDefault().removeStickyEvent(event);//移除粘性事件
                            return;
                        }
                    }
                    //可以使用intent来打开用户登陆界面
                    Intent intentLogin = new Intent(getApplicationContext(), LoginActivity.class);
                    startActivity(intentLogin);
                    EventBus.getDefault().removeStickyEvent(event);//移除粘性事件
                    finish();
                }
            }).show();
        }
    }
}
